//
//  BirthdayAlertView.m
//  us
//
//  Created by karry on 2025/4/1.
//

#import "USDateAlertView.h"

@interface USDateAlertView ()

@property (nonatomic, assign) BOOL limitToNow;
@property (nonatomic, strong) UIButton *doneBtn;

@property (nonatomic ,strong) UIView *alertView;
@property (nonatomic ,strong) UILabel *titleLab;
@property (nonatomic ,strong) UILabel *subTitleLab;

@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) UIDatePicker *picker;

@property (nonatomic ,strong) NSString *subTitle;

@end

@implementation USDateAlertView

 
 
- (instancetype)initWithDate:(NSDate *)date
                 andSubTitle:(NSString *)subTitle
                  limitToNow:(BOOL)limitToNow {
    self = [super init];
    if (self) {
        _limitToNow = limitToNow;
        [self initializationWithDate:date andSubTitle:(NSString *)subTitle];
    }
    return self;
}

- (void)initializationWithDate:(NSDate *)date andSubTitle:(NSString *)subTitle  {
    _date = date;
    _subTitle = subTitle;

    self.frame = [UIScreen mainScreen].bounds;
    UIView *backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    backgroundView.backgroundColor = [UIColor mainBgColor];
    backgroundView.alpha = 0.85;
    UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
    [backgroundView addGestureRecognizer:myTap];
    
    [self addSubview:backgroundView];
    
    [self addSubview:self.doneBtn];
    [self.doneBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(56);
        make.left.mas_equalTo(self.mas_safeAreaLayoutGuideLeft).mas_offset(32);
        make.right.mas_equalTo(self.mas_safeAreaLayoutGuideRight).mas_offset(-32);
        make.bottom.mas_equalTo(self.mas_safeAreaLayoutGuideBottom).mas_offset(-28);
    }];
    
    [self addSubview:self.alertView];
    [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_safeAreaLayoutGuideLeft).mas_offset(32);
        make.right.mas_equalTo(self.mas_safeAreaLayoutGuideRight).mas_offset(-32);
        make.bottom.mas_equalTo(self.doneBtn.mas_top).mas_offset(-28);
    }];
    
    [self.alertView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.alertView);
        make.top.mas_equalTo(self.alertView).mas_offset(16);
    }];
    
    [self.alertView addSubview:self.subTitleLab];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.alertView);
        make.top.mas_equalTo(self.titleLab.mas_bottom).mas_offset(6);
    }];

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = UIColor.borderColor.CGColor;
    shapeLayer.lineWidth = 2;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineDashPattern = @[@8, @8];

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 70)];
    [path addLineToPoint:CGPointMake(ScreenWidth - 64, 70)];
    
    shapeLayer.path = path.CGPath;
    [self.alertView.layer addSublayer:shapeLayer];
    
    self.picker = [[UIDatePicker alloc]init];
    self.picker.datePickerMode = UIDatePickerModeDate;
    self.picker.preferredDatePickerStyle = UIDatePickerStyleWheels;
    if (self.limitToNow) {
        self.picker.maximumDate = [NSDate date];
    }
    self.picker.date = self.date;
    [self.alertView addSubview:self.picker];
    
    [self.picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.subTitleLab.mas_bottom).mas_offset(34);
        make.left.mas_offset(16);
        make.right.mas_offset(-16);
        make.bottom.mas_equalTo(self.alertView.mas_bottom).mas_offset(-20);
        
    }];
    
    
}

- (void)didTapBackgroundView:(id)sender{    
    [self dismss];
}

- (void)doneAction {
    [self dismss];
    self.date = self.picker.date;
    !self.didChooseBlock?:self.didChooseBlock(self.date);
}
 

- (void)showInView:(UIView *)view{
    
    [view addSubview:self];
    
    if (self.superview == nil) {
        return;
    }
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_offset(0);
    }];
    
    _alertView.transform = CGAffineTransformMakeScale(1.2, 1.2);
    _alertView.alpha = 0;
    self.alpha = 0;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alertView.alpha = 1;
        self.alpha = 1;
    } completion:nil];
    
}
- (void)dismss{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UIView *)alertView {
    if (_alertView == nil) {
        _alertView = [UIView new];
        _alertView.backgroundColor = [UIColor mi_1_color];
        _alertView.layer.cornerRadius = 14;
     }
    return _alertView;
}

- (UILabel *)titleLab {
    if (_titleLab == nil) {
        _titleLab = [[UILabel alloc]init];
        _titleLab.text = @"选择日期";
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.textColor = [UIColor jk_colorWithHex:0x1c1c1c];
        _titleLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    }
    return _titleLab;
}

- (UILabel *)subTitleLab {
    if (_subTitleLab == nil) {
        _subTitleLab = [[UILabel alloc]init];
        _subTitleLab.text = self.subTitle;
        _subTitleLab.textAlignment = NSTextAlignmentCenter;
        _subTitleLab.textColor = [UIColor jk_colorWithHex:0x1c1c1c];
        _subTitleLab.font = [UIFont systemFontOfSize:12];
    }
    return _subTitleLab;
}

- (UIButton *)doneBtn {
    if (_doneBtn == nil) {
        _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _doneBtn.layer.cornerRadius = 14;
        _doneBtn.clipsToBounds = YES;
        [_doneBtn setTitle:@"完成了" forState:0];
        [_doneBtn.titleLabel setFont:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium]];
        [_doneBtn setTitleColor:[UIColor blackColor] forState:0];
        [_doneBtn setBackgroundImage:[UIImage jk_imageWithColor:[UIColor mi_1_color]] forState:0];
        [_doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneBtn;
}


@end
